<html>
<head>
<title>{{.Title}}</title>
<link rel="stylesheet" type="text/css" href="res/style.css" />
<link rel="icon" type="image/ico" href="res/favicon.ico" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="res/third_party/jquery.tablednd.js"></script>
<script type="text/javascript">
"use strict";

var issues = {{.BugsJson}};
var edited = {};

function edit_label(bug_id, old_value, new_value) {
    console.log("issue[" + bug_id + "]: " + old_value + " -> " + new_value);
    if (!edited[bug_id]) {
        edited[bug_id] = JSON.parse(JSON.stringify(issues[bug_id]));
    }
    var old_index = edited[bug_id]["labels"].indexOf(old_value);
    if (old_index > -1) {
        edited[bug_id]["labels"][old_index] = new_value;
    } else {
        edited[bug_id]["labels"].push(new_value)
    }
    if (JSON.stringify(issues[bug_id]) == JSON.stringify(edited[bug_id])) {
        console.log("Not changing " + bug_id);
        delete edited[bug_id]
    }
    document.getElementById("all_edits").value = JSON.stringify(edited);
}

</script>
</head>
<body>
<h1>BugChomper</h1>

<form method="post">
<input type="hidden" name="all_edits" id="all_edits" value="{}" />
<input type="submit" value="Submit changes to issue tracker" />
</form>
<table id="buglist">
  <thead>
    <tr id="table_header" class="nodrag tr_head">
      <td colspan=3><h2>Open bugs for {{.User}}</h2></td>
    </tr>
    <tr id="table_subheader" class="nodrag tr_head">
      <td>ID</td>
      <td>Priority</td>
      <td>Title</td>
    </tr>
  </thead>
  <tbody>
    {{with $all_data := .}}
      {{range $index, $priority := index $all_data.Priorities}}
        <tr id="priority_{{$priority}}"
            class="{{if eq $index 0}}nodrop{{else}}{{end}} nodrag priority_row priority_{{$priority}}"
            >
          <td colspan=3 class="priority_td">Priority {{$priority}}</td>
        </tr>
        {{range $index, $bug := index $all_data.BugsByPriority $priority}}
          <tr id="{{$bug.Id}}" class="priority_{{$priority}}">
            <td id="id_{{$bug.Id}}">
              <a href="{{$bug.URL}}" target="_blank">{{$bug.Id}}</a>
            </td>
            <td id="priority_{{$bug.Id}}">{{$priority}}</td>
            <td id="title_{{$bug.Id}}">{{$bug.Title}}</td>
          </tr>
        {{end}}
      {{end}}
    {{end}}
  </tbody>
</table>

<script type="text/javascript">
$(document).ready(function() {
    $("#buglist").tableDnD({
        onDrop: function(table, dropped_row) {
            var id = dropped_row.id;
            var css_priority_prefix = "priority_"
            var new_priority = null;
            var dropped_index = null;
            var thead_rows = table.tHead.rows;
            var tbody_rows = table.tBodies[0].rows;
            var all_rows = [];
            for (var i = 0; i < thead_rows.length; i++) {
                all_rows.push(thead_rows[i]);
            }
            for (var i = 0; i < tbody_rows.length; i++) {
                all_rows.push(tbody_rows[i]);
            }
            for (var i = 0; i < all_rows.length; i++) {
                if (all_rows[i].id) {
                    if (all_rows[i].id.indexOf(css_priority_prefix) == 0) {
                        new_priority = all_rows[i].id.substring(css_priority_prefix.length);
                    }
                    if (all_rows[i].id == id) {
                        break;
                    }
                } else {
                  console.warn("No id for:");
                  console.warn(all_rows[i]);
                }
            }
            if (new_priority) {
                priority_td = document.getElementById(css_priority_prefix + id);
                old_priority = priority_td.innerHTML;
                if (priority_td && new_priority != old_priority) {
                    priority_td.innerHTML = new_priority;
                    document.getElementById(id).className = css_priority_prefix + new_priority;
                    edit_label(id, "{{.PriorityPrefix}}" + old_priority, "{{.PriorityPrefix}}" + new_priority);
                }
            }
        }
    });
});
</script>
</body>
</html>
